﻿<%@ WebHandler Language="C#" Class="ajax" %>
using System;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using jsm.SqlHelper;
using dbcaac;

public class ajax : IHttpHandler
{
    private HttpContext context;

    public void ProcessRequest (HttpContext context) {

        this.context = context;

        // 取得页面的数据实体
        var entity = EntityFactory.getEntity(pageName);
        
        // 取不到实体，抛出404错误
        if (null == entity)
        {
            ExceptionHelper.Throw404Exception(context);
        }
        
        // 载入表单数据
        entity.loadPageData(context);

        // 检查数据有效性，删除时不用检查
        string errorMsg = null;
        if ("DELETE" != actionName.ToUpper())
        {
            errorMsg = entity.isValid();
        }
        
        // 如果数据有效就执行请求的操作
        if (null == errorMsg) {
            if (entity.executePageAction(actionName) <= 0) {
                errorMsg = "操作失败，请重试";
            }
        }
        
        string json = null;

        // 数据有效并且操作成功
        if (null == errorMsg)
        {
            json = @"{""total"":""true""}";
        }
        // 数据无效或者操作失败
        else {
            //json = @"{""errorMsg"":""操作失败，请重试！""}";
            json = @"{""errorMsg"":" + errorMsg + @"}";
        }
        
        context.Response.ContentType = "text/plain";
        context.Response.Write(json);
        context.Response.End();
    } 
    
    public bool IsReusable {
        get { return false; }
    }

    protected string actionName
    {
        get { return this.context.Request["action"]; }
    }

    protected string pageName
    {
        get { return this.context.Request["view"]; }
    }

}